package com.atung.controller;

import java.util.List;
import java.util.Objects;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atung.request.RequestCompanion;
import com.atung.request.RequestPersonalMessage;
import com.atung.response.ResponseCompanion;
import com.atung.response.ResponsePersonalMessage;
import com.atung.service.PersonalCenterService;
import com.atung.tool.ParamVerificationTool;
import com.yuyi.full.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/centre")
@Api(tags="个人中心")
public class PersonalCenterController {
	@Autowired
	private PersonalCenterService personalCenterService;
	
	
	@GetMapping("/getMessage")
	@ApiOperation("获取我的个人信息")
	public Result<?> personalMessage(@RequestParam String openId)throws Exception {
		ResponsePersonalMessage response=personalCenterService.selectPersonalMsgAndOpenId(openId);
		return Result.success(response);
	}
	
	@GetMapping("/getCompanion")
	@ApiOperation("获取同行人员")
	public Result<List<ResponseCompanion>> getCompanion(@RequestParam String openId){
		List<ResponseCompanion> response=personalCenterService.selectCompanionAndOpenId(openId);
		return Result.success(response);
	}
	
	@PostMapping("/inseterCompanion")
	@ApiOperation("新增同行人员")
	public Result inseterCompanion(@Valid @RequestBody RequestCompanion request)throws Exception{
		if(!ParamVerificationTool.phoneVerification(request.getCompanionPhone())){
			Result.error(202, "ERR PHONE IS ILLEGALITY");
		};
		Boolean response=personalCenterService.insertCompanion(request);
		if(!response){
			Result.error(202, "新增失败");
		}
		return Result.success();
	}
	
	@PostMapping("/updataMessage")
	@ApiOperation("修改我的个人信息")
	public Result<?> updataMessage(@Valid @RequestBody RequestPersonalMessage request) {
		if(Objects.nonNull(request.getUserPhone())){
			if(!ParamVerificationTool.phoneVerification(request.getUserPhone())){
				//手机号为非法的
				return Result.error(202, "ERR PHONE IS ILLEGALITY");
			};
		}
		Boolean response=personalCenterService.updataMessageAndOpenId(request);
		if(!response){
			return Result.error(202, "ERR USER IS NULL");
		}
		return Result.success(response);
	}
	
	@PostMapping("/updataCompanion")
	@ApiOperation("修改同行人信息")
	public Result updataCompanion(@RequestBody RequestCompanion request){
		if(Objects.isNull(request.getId())){
			return Result.error(202, "ERR ID IS NULL");
		}
		if(Objects.nonNull(request.getCompanionPhone())){
			if(!ParamVerificationTool.phoneVerification(request.getCompanionPhone())){
				return Result.error(202, "ERR PHONE IS ILLEGALITY");
			};
		}
		Boolean response=personalCenterService.updateCompanion(request);
		if(!response){
			return Result.error(202, "ERR  UPDATA EXCEPTION,PLEASE AGAIN?");
		}
		return Result.success();
	}
	
	@PostMapping("/delectCompanion")
	@ApiOperation("删除同行人")
	public Result<?> delectCompanion(@RequestParam Long id){

		Boolean response=personalCenterService.delectCompanionAndId(id);
		if(!response){
			return Result.error(202, "ERR  DELETE EXCEPTION,PLEASE AGAIN?");
		}
		return Result.success();
	}
	
}
